package be.kul.osgi.ds.myagent.message;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import be.kul.osgi.ds.irole.IRole;
import be.kul.osgi.ds.messageservice.MessageObject;
import be.kul.osgi.ds.myagent.MyAgent;
import be.kul.osgi.ds.myagent.Role.RoleHandler;

public class GiveRole implements BasicBehaviour {

	public String loc;
	public String role;
	public String id;
	public MyAgent agent;
	
	public GiveRole(String id, MyAgent agent){
		this.agent = agent;
		this.id = id;
	}
	
	@Override
	public String getID() {
		// TODO Auto-generated method stub
		return this.id;
	}

	@Override
	public void HandleMessage(MessageObject mo) {
		// TODO Auto-generated method stub
		if(mo.getPerformative()==MessageObject.ASKROLE){
			String receiver = mo.getSender();
			System.out.println("@GiveRole - Henndlemessage : " + receiver);
			this.role = mo.getContent();
			
			//get Role
			IRole r = this.agent.getRoleFromName(role);
			
			
			RoleHandler rh = new RoleHandler(r, this.agent);
			//pack Role
			String filename = null;
			
			boolean exist =false;
			try {
				final URL url = new URL("http://localhost:" + this.agent.getPort()+"/files/" + this.role + ".zip");
			    HttpURLConnection huc = (HttpURLConnection) url.openConnection();
			    int responseCode = huc.getResponseCode();
				if (responseCode == HttpURLConnection.HTTP_OK){
					exist = true	;
					System.out.println("it exists !!!");
				}
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!exist){
				if (agent.BundleLookup(this.role)){
					try {
						filename = agent.serverservice.publish(agent.bundledata, rh.packBundle(agent.bundledata));
						loc = "http://localhost:" + this.agent.getPort() + "/files/" + filename;
					
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else {
				loc =  "http://localhost:" + this.agent.getPort() + "/files/" + this.role + ".zip";
				filename =  this.role + ".zip";
			}
		//	System.out.println("@GiveRole - Handlemessage!!!");
		//	System.out.println(filename);
		//	System.out.println(loc);
			//Construct URL
			
			if (filename != null){
				MessageObject mes = new MessageObject(MessageObject.GIVEROLE);
				mes.setReceiver(receiver);
				mes.setSender(this.agent.name);
				mes.setContent(loc);
				mes.setConversationId(this.id);
				String message = agent.getMessageService().serialize(mes);
				
				System.out.println(message);
				
				agent.getComm().sendmulticast(message);
			}
			
		}
	}

	@Override
	public boolean isInterested(MessageObject mo) {
		// TODO Auto-generated method stub
		if(mo.getConversationId().equals(this.id) && mo.getPerformative()==MessageObject.ASKROLE){
			return true;
		}
		return false;
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		
	}

}
